#!/usr/bin/python3
import sys

# part 1

neighbors = [(-1, 0, 0), (1, 0, 0), (0, -1, 0), (0, 1, 0), (0, 0, -1), (0, 0, 1)]
def reduce_sides(construct, x, y, z):
    covered = 0
    for nx, ny, nz in neighbors:
        if (x + nx, y + ny, z + nz) in construct:
            covered += 1
    return 2 * covered

def part1():
    construct = []
    sides = 0
    for line in sys.stdin:
        x, y, z = [int(num) for num in line.strip().split(',')]
        sides += 6 - reduce_sides(construct, x, y, z)
        construct.append((x, y, z))
    print(f'{sides} sides covered')
    return construct, sides

# part 2

def recursive_flood_fill(construct, visited, x, y, z, maxx, maxy, maxz):
    if x < 0 or y < 0 or z < 0: return
    if x > maxx or y > maxy or z > maxz: return

    point = (x, y, z)
    if point in visited or point in construct: return

    #print('flooded', x, y, z)
    visited.append((x, y, z))
    for nx, ny, nz in neighbors:
        flood_fill(construct, visited, x + nx, y + ny, z + nz, maxx, maxy, maxz)

def flood_fill(construct, visited, x, y, z, maxx, maxy, maxz):
    stack = [(x, y, z)]
    while len(stack) > 0:
        point = cx, cy, cz = stack.pop()
        if cx < 0 or cy < 0 or cz < 0: continue
        if cx > maxx or cy > maxy or cz > maxz: continue
        if point in visited or point in construct: continue
        visited.append(point)
        for nx, ny, nz in neighbors:
            stack.append((cx + nx, cy + ny, cz + nz))

def get_trapped_air_sides(construct):
    minx = miny = minz = 0
    maxx = maxy = maxz = 0
    for x, y, z in construct:
        maxx = max(x, maxx)
        maxy = max(y, maxy)
        maxz = max(z, maxz)
    print(maxx, maxy, maxz)

    visited = []
    # only positive integers
    flood_fill(construct, visited, 0, 0, 0, maxx + 1, maxy + 1, maxz + 1)

    air = []
    sides = 0
    for x in range(maxx):
        for y in range(maxy):
            for z in range(maxz):
                point = (x, y, z)
                if not (point in construct or point in visited):
                    print(f'air found at ({x}, {y}, {z})')
                    sides += 6 - reduce_sides(air, x, y, z)
                    air.append((x, y, z))
    return sides

def part2():
    construct, sides = part1()
    sides -= get_trapped_air_sides(construct)
    print(f'{sides} sides covered without trapped air considered')

if sys.argv[1] in '1':
    part1()
else:
    part2()
